﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace TKT.Lifter.CIM.Lifter.TibcoMsg
{
    public class MessageReturnSet
    {
        public string ReturnCode { get; set; }
        public string ReturnMessage { get; set; }

        public XElement ToXElement()
        {
            XElement returnSet = new XElement("Return");
            returnSet.Add(new XElement("RETURNCODE", this.ReturnCode));
            returnSet.Add(new XElement("RETURNMESSAGE", this.ReturnMessage));
            return returnSet;
        }

        public void Parse(XElement returnSet)
        {
            this.ReturnCode = returnSet.Element("RETURNCODE").Value;
            this.ReturnMessage = returnSet.Element("RETURNMESSAGE").Value;
        }

        public void Parse(TIBCO.Rendezvous.Message message)
        {
            TIBCO.Rendezvous.MessageField field = message.GetField("Message");
            if (field == null)
            {
                field = message.GetField("xmlData");
            }
            string strXML = field.Value as string;
            XElement root = XElement.Parse(strXML);
            XElement returnSet = root.Element("Return");
            this.Parse(returnSet);
        }
    }
}
